package de.preisrobot.business.parser.amazon;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;

import org.apache.log4j.Logger;

import de.preiscrawler.AlleAnbieter;
import de.preisrobot.business.parser.AbstractParser;
import de.preisrobot.entity.Artikel;
import de.preisrobot.service.internal.PreisRobotParserException;

public class AmazonParser extends AbstractParser
{

   private static Logger log = Logger.getLogger(AmazonParser.class);

   public List<Artikel> parse(Source source) throws PreisRobotParserException 
   {
      List<Artikel> list = new ArrayList<Artikel>();

      Element elementById = source.getElementById("btfResults");

      if (elementById == null)
         throw new PreisRobotParserException();

      List<Element> elementList = elementById.getAllElementsByClass("result product");

      for (Element element : elementList)
      {
         Artikel artikel = new Artikel();
         
         try{
          
            AmazonListTemplateParserV1  templatev1= new AmazonListTemplateParserV1(element);
            artikel = templatev1.parse();
         }
         catch (Exception e) {
           log.warn(e.getMessage());
         }

         String itemName = element.getAttributeValue("name");
         artikel.setArtikelNr(itemName);

         List<Element> priceTag = element.getAllElementsByClass("newPrice");
         List<Element> titles = element.getAllElementsByClass("productTitle");
         
         // 2. Alternative Warum auch immer?
         List<Element> imageDiv  = element.getAllElementsByClass("image");
         
         if (priceTag != null && !priceTag.isEmpty())
            getPrice(priceTag.get(0), artikel);

         if (titles != null && !titles.isEmpty())
         {
            getProductLine(titles.get(0), artikel);
         }
         else if(!imageDiv.isEmpty())
         {
            getProductLine(imageDiv.get(0), artikel);
         }

         list.add(artikel);
      }

      return list;
   }

   private void getPrice(Element element, Artikel artikel)
   {
      element.getFirstElement();
      List<Element> allElements = element.getAllElements();
      String[] arr = new String[3];

      for (Element element2 : allElements)
      {
         String name = element2.getName();

         if (name.contains("span"))
         {
            // Sring imagePath =getImage(element2);
            String attributeValue = element2.getTextExtractor().toString();
            arr[0] = attributeValue;
         }
         else if (name.startsWith("a"))
         {
            String attributeValue = element2.getAttributeValue("href");
            String attributeValue2 = element2.getTextExtractor().toString();
            arr[1] = attributeValue;
            arr[2] = attributeValue2;
         }

      }

      artikel.setPreis(arr[0]);
      artikel.setArtikelPreisRef(arr[1]);
      artikel.setPreisDesc(arr[2]);
   }

   private void getProductLine(Element element, Artikel artikel)
   {

      Element firstElement = element.getFirstElement();
      List<Element> allElements = firstElement.getAllElements();
      String[] arr = new String[3];

      for (Element element2 : allElements)
      {
         String name = element2.getName();

         if (name.contains("img"))
         {
            // Sring imagePath =getImage(element2);
            String attributeValue = element2.getAttributeValue("src");
            arr[0] = attributeValue;
         }
         else if (name.startsWith("a"))
         {
            String attributeValue = element2.getAttributeValue("href");
            String attributeValue2 = element2.getTextExtractor().toString();
            arr[1] = attributeValue;
            arr[2] = attributeValue2;
         }

      }
      artikel.setImageLink(arr[0]);
      artikel.setArtikelLink(arr[1]);
      artikel.setDesc(arr[2]);
      System.out.println(firstElement.getName() + " " + arr[0] + " " + arr[1] + " " + arr[2]);
   }

   @Override
   public void parseStream(List<Artikel> list, InputStream in) throws IOException, PreisRobotParserException
   {
         List<Artikel> list2 = parse(new Source(in));
         list.addAll(list2);
   }
   
   @Override
   public void parseStream(List<Artikel> list, Reader in) throws IOException, PreisRobotParserException
   {
         List<Artikel> list2 = parse(new Source(in));
         list.addAll(list2);
   }

   @Override
   public int getAnbieter()
   {
      return AlleAnbieter.AMAZON;
   }
}
